class_name PlayerStateMoving
extends PlayerState
	
func _process(_delta: float) -> void:
	if player.control_scheme == Player.ControlScheme.CPU:
		ai_behavior.process_ai()
	else:
		handle_movement()
	player.set_movement_animation()
	player.set_heading()
		
func handle_movement() -> void:
	var direction := KeyUtils.get_input_vector(player.control_scheme)
	player.velocity = direction * player.speed
	
	if player.velocity != Vector2.ZERO:
		teammate_detection_area.rotation = player.velocity.angle()
		
	if KeyUtils.is_action_just_pressed(player.control_scheme, KeyUtils.Action.PASS):
		if player.has_ball():
			state_transition(Player.State.PASSING)
		elif can_teamate_pass_ball():
			ball.carried.get_pass_request(player)
		else:
			player.swap_player(player)
	elif KeyUtils.is_action_just_pressed(player.control_scheme, KeyUtils.Action.SHOOT):
		if player.has_ball():
			state_transition(Player.State.PREPPING)
		elif ball.can_air_interact():
			if player.velocity == Vector2.ZERO:
				if player.is_face_target_goal():
					state_transition(Player.State.VOLLEY_KICK)
				else:
					state_transition(Player.State.BICYCLE_KICK)
			else:
				state_transition(Player.State.HEADER)
		elif player.velocity != Vector2.ZERO:
			state_transition(Player.State.TACKLING)
				
func can_carry_ball() -> bool:
	return player.role != Player.Role.GOALIE
	
func can_teamate_pass_ball() -> bool:
	return ball.carried != null and ball.carried.country == player.country and ball.carried.is_cpu()
	
func can_pass_ball() -> bool:
	return true
